var regs = $A();
dwr.engine.setPreHook(function() {
    $("spinner").show();
});
dwr.engine.setPostHook(function() {
    $("spinner").hide();
});

function addRegistration() {
	if(validateRegistration()) {
		if($("checkCC").checked == true) {
			var reg = createRegistration();
			reg["raceType"] = "Type1";
			regs[regs.length] = reg;
		}
		if($("checkJump").checked == true) {
			var reg = createRegistration();
			reg["raceType"] = "Type2";
			regs[regs.length] = reg;
		}
		clearForm();
		drawTable(); 
	}
}

function validateRegistration(reg) {
	return true;
	if(undef($("firstName").value) || undef($("lastName").value) || undef($("birthYear").value) || undef($("gender").value))
	{
		alert("Required field has not been entered, please check again");
		return false;
	} else {
		if((!$("checkCC").checked) && (!$("checkJump").checked)) {
			alert("You must choose at least one Type");
			return false;
		}
		return true;
	}
}

function undef(string) {
	if((string == undefined) || (string == null) || (string.length < 1)) {
		return true;
	} else {
		return false;
	}
}

function createRegistration() {
	var reg = $H();
	reg["firstName"] = $("firstName").value;
	reg["lastName"] = $("lastName").value;
	reg["birthYear"] = $("birthYear").value;
	reg["gender"] = $("gender").value;
	reg["email"] = $("email").value;
	reg["address"] = $("address").value;
	reg["postCode"] = $("postCode").value;
	reg["phoneNumber"] = $("phoneNumber").value;
	return reg;	
}

function clearForm() {
	$("firstName").value = "";
	$("lastName").value = "";
	$("birthYear").value = "";
	$("gender").value = "";
	$("checkCC").checked = false;
	$("checkJump").checked = false;
	$("email").value = "";
	$("address").value = "";
	$("postCode").value = "";
	$("phoneNumber").value = "";
}


function registrationToHTML(reg, detailedView) {
	var tr = new Element("TR");
	if(detailedView) {
		tr.appendChild(new Element("TD").update(reg["id"]));
	}
	tr.appendChild(new Element("TD").update(reg["firstName"]));
	tr.appendChild(new Element("TD").update(reg["lastName"]));
	tr.appendChild(new Element("TD").update(reg["birthYear"]));
	tr.appendChild(new Element("TD").update(reg["gender"]));
	tr.appendChild(new Element("TD").update(reg["raceType"]));
	if(detailedView) {
		tr.appendChild(new Element("TD").update(reg["email"]));
		tr.appendChild(new Element("TD").update(reg["address"]));
		tr.appendChild(new Element("TD").update(reg["postCode"]));
		tr.appendChild(new Element("TD").update(reg["phoneNumber"]));
		tr.appendChild(new Element("TD").update(reg["insertDate"]));
	}
	if(!detailedView) {
		tr.appendChild(new Element("TD").update(createDeleteLink()));
	}
	tr.registration = reg;
	return tr;
}
function registrationsToHTML(detailedView) {
	if(regs.length == 0) {
		var text = new Element("SPAN");
		text.update("No registrations in list, add registration above.");
		return text;
	} else {
		var table = new Element("TABLE", {"class" : "registrationsTable sortable", "id" : "sortabletable"});
		//table.id = "sortabletable";
		var thead = new Element("THEAD");
		var tbody = new Element("TBODY");
		table.appendChild(thead);
		table.appendChild(tbody);
		thead.appendChild(createTableHeader(detailedView));
		regs.each(function(item) {
			tbody.appendChild(registrationToHTML(item, detailedView));
		});
		return table;
	}
}

function createTableHeader(detailedView) {
	var tr = new Element("TR");
	if(detailedView) {
		tr.appendChild(createTH().update("Id"));
	}
	tr.appendChild(createTH().update("First Name"));
	tr.appendChild(createTH().update("Last Name"));
	tr.appendChild(createTH().update("Year"));
	tr.appendChild(createTH().update("Gender"));
	tr.appendChild(createTH().update("Type"));
	if(detailedView) {
		tr.appendChild(createTH().update("Email"));
		tr.appendChild(createTH().update("Adresse"));
		tr.appendChild(createTH().update("Postal Number"));
		tr.appendChild(createTH().update("Phone Number"));
		tr.appendChild(createTH().update("Reg. Date"));
	}
	//if((detailedView == undefined) || !detailedView) {
		tr.appendChild(createTH().update('&nbsp;'));
	//}
	return tr;
}

function createTH() {
	return new Element("TH", {"class" : "registrationsTH"});
}

function drawTable(detailedView) {
	$("registrationsDiv").update(registrationsToHTML(detailedView));
	if($("submitButton") != undefined) {
		if(regs.length > 0) {
			$("submitButton").disabled = false;
		} else {
			$("submitButton").disabled = true;
		}
	}
	if(message.length > 0) {
		$("messageText").update(message);
		message = "";
	}
	if(error.length > 0) {
		$("errorText").update(error);
		error = "";
	}
}

function submitRegistrations() {
	var regNummer = "";
	Service.submitRegistrations(regs);
	clearForm();
	setMessage("Registration has been created");
	drawTable(false);
	$("entryDiv").hide();	
	$("submitButton").hide();	
	$("instructionsDiv").hide();	
	//$("infoDiv").show();	
	$$(".deleteLink").each(function(item) {
		item.hide() 
	});
}

function createDeleteLink() {
	var img = new Element("IMG");
	img.src = "images/delete.gif";
	img.title = "Slett";
	var a = new Element("A", {'href': '#', 'class':'deleteLink'});
	a.appendChild(img);
	a.observe("click", function(event) {
		deleteRow(event);
	});
	return a;
}

function deleteRow(event) {
	alert('deleting row');
	var row = $(event.target).up("tr");
	var td = $(event.target).getOffsetParent();
	var registration = row.registration;
	regs = regs.without(registration);
	drawTable();
}

var message = "";
var error = "";
function setMessage(userMessage) {
	message = userMessage;
}
function setError(errorMessage) {
	error = errorMessage;
}
function loadData(detailedView) {
	Service.getAllRegistrations(function(registrations) {
		regs = registrations;
		drawTable(detailedView);
		sortables_init();
	});
}

